package com.bytz.modules.cms.reseller.service.impl;

import cn.hutool.core.text.PasswdStrength;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.reseller.service.IResellerPasswordService;
import com.bytz.modules.cms.reseller.service.IResellerService;
import com.bytz.modules.cms.reseller.util.ResellerSecurityUtils;
import com.bytz.modules.system.security.vo.LoginReseller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ResellerPasswordServiceImpl implements IResellerPasswordService {

    @Autowired
    private IResellerService resellerService;

    @Override
    public Result<?> resetPassword(String username, String oldpassword, String newpassword, String confirmpassword) {

        LoginReseller user = ResellerSecurityUtils.getLoginUser();
        if (user == null) {
            return Result.error("用户不存在！");
        } else if (!ResellerSecurityUtils.matchesPassword(oldpassword, user.getPassword())) {
            return Result.error("旧密码输入错误!");
        } else if (PasswdStrength.getLevel(newpassword).compareTo(PasswdStrength.PASSWD_LEVEL.STRONG) < 0) {
            return Result.error("新密码强度不够!");
        } else if (!newpassword.equals(confirmpassword)) {
            return Result.error("两次输入密码不一致!");
        } else {
            String password = ResellerSecurityUtils.encryptPassword(newpassword);
            resellerService.updatePassword(user.getId(), username, password);
            return Result.ok("密码重置成功!");
        }
    }
}
